package com.example.yuejianapp14.Activity.Fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.example.yuejianapp14.Activity.Activity.SearchActivity;
import com.example.yuejianapp14.Activity.Adapter.ClassAdapter;
import com.example.yuejianapp14.Activity.JavaBean.class1;
import com.example.yuejianapp14.Activity.Listener.ClassListener;
import com.example.yuejianapp14.Activity.Model.ClassModel;
import com.example.yuejianapp14.R;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassFragment extends Fragment {
    private RecyclerView recyclerView;
    private List mDataList;
    private View view;
    private ImageButton classfind;


    public ClassFragment() {
        // Required empty public constructor
    }

    ClassListener classListener = new ClassListener() {
        @Override
        public void onResponse(List<class1> beanList) {
            mDataList=beanList;
            ClassAdapter adapter = new ClassAdapter(getActivity(),mDataList);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_class, container, false);
        initRecyclerView();
        getData("0");
        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        final Button rumen =(Button) getActivity().findViewById(R.id.rumen);
        final Button cainiao =(Button) getActivity().findViewById(R.id.cainiao);
        final Button jinjie =(Button) getActivity().findViewById(R.id.jinjie);
        final Button all =(Button) getActivity().findViewById(R.id.all);
        final ImageButton classfind=(ImageButton) getActivity().findViewById(R.id.classfind);

        classfind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });


        rumen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initRecyclerView();
                getData("1");
                rumen.setTextColor(Color.parseColor("#66ff33"));
                cainiao.setTextColor(Color.parseColor("#000000"));
                jinjie.setTextColor(Color.parseColor("#000000"));
                all.setTextColor(Color.parseColor("#000000"));

            }
        });

        cainiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initRecyclerView();
                getData("2");
                rumen.setTextColor(Color.parseColor("#000000"));
                cainiao.setTextColor(Color.parseColor("#66ff33"));
                jinjie.setTextColor(Color.parseColor("#000000"));
                all.setTextColor(Color.parseColor("#000000"));

            }
        });

        jinjie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initRecyclerView();
                getData("3");
                rumen.setTextColor(Color.parseColor("#000000"));
                cainiao.setTextColor(Color.parseColor("#000000"));
                jinjie.setTextColor(Color.parseColor("#66ff33"));
                all.setTextColor(Color.parseColor("#000000"));

            }
        });

        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initRecyclerView();
                getData("0");
                rumen.setTextColor(Color.parseColor("#000000"));
                cainiao.setTextColor(Color.parseColor("#000000"));
                jinjie.setTextColor(Color.parseColor("#000000"));
                all.setTextColor(Color.parseColor("#66ff33"));

            }
        });

    }



    private void getData(String diffculty) {
        ClassModel classModel =new ClassModel();
        classModel.selectclass(diffculty,classListener);
    }

    private void initRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_class1);
        recyclerView.setLayoutManager(new LinearLayoutManager((this.getActivity())));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

}
